$(function () {
    //重新加载二维码
    function randomCode() {
        var path = ip+"/Back/Admin/VerifyCode?tm=" + Math.random();
        $(".codeImg img").attr('src', path);
    }
    //点击刷新二维码
    $(".codeImg").click(function () {
        randomCode();
    });
    //登录
    $("#LoginBtn").click(function () {
        var _loginName = $("#loginName").val();
        var _password = $("#password").val();
        var _checkCode = $("#checkCode").val();
        // $.get("http://192.168.83.133:8088/Back/Admin/Login",{'loginName':_loginName,'password':hex_md5(_password+salt),'checkCode':_checkCode},function (data) {
        //     console.log(data)
        // })
        // var timestamp=new Date().getTime();
        // var url = 'http://192.168.83.133:8088/Back/Admin/Login?timestamp='+timestamp;
        // alert(hex_md5("0"+salt));
        $.ajax({
            type: "POST",
            url: ip + '/Back/Admin/Login',
            headers: {"Content-Type": "application/json", "Accept": "application/json;charset=utf-8"},
            data: JSON.stringify({
                "checkCode": _checkCode,
                "loginName": _loginName,
                "password": hex_md5(_password + salt)
            }),
            xhrFields:{withCredentials: true},
            // data:{
            //     "checkCode": _checkCode,
            //     "loginName": _loginName,
            //     "password": hex_md5(_password+salt)
            //     // MultipartFile:{
            //     //     "checkCode": _checkCode,
            //     //     "loginName": _loginName,
            //     //     "password": hex_md5(_password+salt)
            //     // }
            // },
            // data:{loginName :_loginName,password :hex_md5(_password+salt),checkCode:_checkCode},
            // jsonp: "callBack",
            // jsonpCallback:'success_jsonpCallback',
            dataType: 'json',
            success: function (data) {
                if (data.code == 1) {
                    var token1 = data.data.token;
                    var _token = encodeURIComponent(token1);
                    SetCookie("loginName",_loginName);
                    SetCookie("token",_token);
                    data.code == "1" ? window.location.href = "Index.html?loginName=" + _loginName + "&token=" + _token : alert(data.message);randomCode();$("#password").val("");$("#checkCode").val()
                }
                else{
                    alert(data.message);
                    // if(data.message=='验证码错误'){
                        var path = ip+"/Back/Admin/VerifyCode?tm=" + Math.random();
                        $(".codeImg img").attr('src', path);
                    // }
                }
            },
            error: function () {
                alert('登录出错');
            }
        });
    });
    //回车
    $(document).keydown(function(event){
        if(event.keyCode==13){            
            $("#LoginBtn").click();                
        }
    });
});